<?php
namespace app\post\controller\home;
use addons\comments\model\CommentsPostModel;
use app\admin\logic\SubscribeLogic;
use app\common\controller\Home;
use app\index\controller\Base;
use app\post\model\Category;
use app\post\model\CollectionModel;
use app\post\model\Post;
use think\Request;
class Index extends Base
{
    public function index()
    {

        $where = [];
        $category = $this->request->param("category",0,"intval");
        if($category!=0)
        {
            array_push($where,['c.category_id',"eq",$category]);
        }

        $articleList  =  Post::where("post_status",1)
            ->alias("a")
            ->join("cp_portal_category_post c","c.post_id = a.id")
            ->join("cp_portal_tag_post t","t.post_id = a.id")
            ->order("update_time desc")
            ->with("user")
            ->field("a.user_id,a.post_excerpt,a.thumbnail,a.more,a.post_title,a.id,a.update_time,a.download_number,a.post_hits,a.post_source,a.post_hits")
            ->where($where)
            ->group("a.id")
            ->paginate();

        $hotArticleList = Post::order("post_hits desc")
            ->field("id,post_title")
            ->limit(15)
            ->select();

        $roundArticle = Post::order("post_hits desc")
            ->field("id,post_title")
            ->limit(round(15))
            ->select();

        $categoryList = Category::field("id,parent_id pid,alias,name")->select();

        $this->assign([
            "categoryList"  => findChild(0,1,$categoryList),
            "articleList"   => $articleList,
            "hotArticleList"=> $hotArticleList,
            "roundArticle"  => $roundArticle
        ]);

        return $this->fetch();
    }

    public function content($id)
    {
        $article = Post::where("id",$id)
            ->find();
        $categoryList = Category::field("id,parent_id pid,alias,name")->select();

        if($article)
        {
            //查看次数
            Post::where("id",$id)->setInc("post_hits",1);
        }

        $article['post_content'] = cmf_replace_content_file_url($article['post_content'],false);

        $nexArticle = Post::where("id",">",$id)->order("id asc")->field("id,post_title")->find();
        $preArticle = Post::where("id","<",$id)->order("id asc")->field("id,post_title")->find();

        $article['collection_status'] = 0;

        //登录下才有判断是否已经收藏
        if(_get_current_user_id() && isset($article['id']))
        {
            $collection = CollectionModel::where("user_id",_get_current_user_id())
                ->where("post_id",$article['id'])
                ->where("is_collection",1)
                ->find();
            if($collection)
            {
                $article['collection_status'] = 1;
            }
        }

        $this->assign([
            "article"   => $article,
            "categoryList"  => findChild(0,1,$categoryList),
            "preArticle" => $preArticle,
            "nexArticle" => $nexArticle,
        ]);

        return $this->fetch();
    }

    public function collection()
    {
        $articleId= $this->request->param("id",0);

        if(!_get_current_user_id()) $this->error("请先登录之后在进收藏");

        if($articleId)
        {
            $collection = $this->request->param("is_collection");
            $article = CollectionModel::where("post_id",$articleId)
                ->where("user_id",_get_current_user_id())
                ->find();
            if($article)
            {
               $res = CollectionModel::where("post_id",$articleId)
                   ->where("user_id", _get_current_user_id())
                   ->update([
                        "is_collection" => $collection
                   ]);
            }else{
                $res = CollectionModel::create([
                        "post_id" => $articleId,
                        "user_id" => _get_current_user_id()
                    ]);
            }
            if($res)
            {
                $this->success("操作成功");
            }else{
                $this->error("操作失败");
            }
        }else{
            $this->error("收藏失败");
        }
    }

    public function subscribe()
    {
        if($this->request->isPost())
        {
            if(!_get_current_user_id()) $this->error("请登录订阅");
            $user = _get_current_user();
            $cid = $this->request->param("category");
            if(!$cid) $this->error("请选择一个分类");

            $cid =array_unique($cid);

            $idList = [];
            $nameArr= [];

            foreach ($cid as $k=>$v){
                $a = explode("_",$v);
                array_push($idList,$a[0]);
                array_push($nameArr,$a[1]);
            }
            $res =  (new SubscribeLogic())->createSubscribe($user['id'],"article",[
                [
                    "title" => "文章分类",
                    "value" => implode(",",$idList),
                    "drive" => "\\app\\post\\model\\Category",
                    "desc"  => implode(",",$nameArr)
                ]
            ]);
            if ($res)
            {
                $this->success("订阅成功");
            }else{
                $this->error("订阅错误");
            }

        }
    }
}